
package controllers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import view.PurchaseHistoryGUI;
import client.CliMessage;


	/**
	 * This Class is a Purchase History Controller which extends AbstractController
	 * @author Tal 
	 *
	 */

	public class PurchaseHistoryController extends AbstractController
	{	
				private PurchaseHistoryGUI purHistoryView;
			  
				
				/**
				 * This is Purchase History  constructor.
				 * @param purHistoryView is a Purchase History window GUI instance.
				 */
				public PurchaseHistoryController(PurchaseHistoryGUI purHistoryView) 
				{
					super(purHistoryView,null);
					this.purHistoryView = purHistoryView;
			
				}
				
				
				
				public String getServiceByName(int service)
				{
					String ServiceName=null;
					  switch (service) {
					    case 1:  ServiceName = "Buy Photo ";
	                     break;  
			            case 2:  ServiceName = "Photo Package";
			                     break;
			            case 3:  ServiceName = "Buy Sample";
	                     break;
			            case 5:  ServiceName = "Daily Photo";
			                     break;
			            case 6:  ServiceName = "Business Layer";
			                     break;
			            case 7:  ServiceName = "Infrastructure Layer";
			                     break;         
			            default: ServiceName = "Invalid Service";
			                     break;
			        }
					  
					  return ServiceName;
					  
				}
				
				

				@Override
				public void handleDBRequestSuccess()
				{
					purHistoryView.displayWrnMessage("Purchase History Found:");
					
				}
				
				@Override
				public void handleDBSearchResult(CliMessage dbResult) 
				{	
					//get a list of all purchase history
					ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
					int rows=srs.size();
					int column=5;
					
					//create data table
					 Object[][] data=new Object[rows][column];
				   	
					 String[] columnNames={"Invoice Id","Service","Price","Credir Card","Date"};

					
					for(int i = 0 ; i <rows  ; i++)
					{
						Map<String, String> purHisList  = srs.get(i);	
			
				
						//invoice_id
						 data[i][0]=purHisList.get("INVOICE_NUMBER");	
						 
						 int service=Integer.valueOf(purHisList.get("SERVICE_ID")); 
							//service
						data[i][1]=getServiceByName(service);
							
						//Price
						 data[i][2]=purHisList.get("PRICE_PAID");
						 //credit
						 data[i][3]=purHisList.get("CREDITNUM");
						 //date
						 data[i][4]=purHisList.get("TIMESTAMP");
						 
							
						 System.out.println("Invoice id: "+purHisList.get("INVOICE_NUMBER")+"Service: "+ purHisList.get("SERVICE_ID") + "Price: "+purHisList.get("PRICE_PAID")+"Credit: "+purHisList.get("CREDITNUM")+"Date: "+purHisList.get("TIMESTAMP"));
							 					
					}	
					
					//set new data table
					purHistoryView.setTable(columnNames, data);
					

					
					//display main window
					purHistoryView.setVisible(true);
					purHistoryView.setTableVisibilty(true);	
							
				}

				@Override
				public void handleNullSearchResult() 
				{
					//display main window
					purHistoryView.setVisible(true);
					purHistoryView.disposeTable();
					
					purHistoryView.displayWrnMessage("Purchase History Not Found!!!");
					
				}




}
